﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using CXMLStudioIDE.Common;
using CXMLStudioIDE.DataAccessLayer;
using System.Windows.Forms;
// Assembly marked as compliant.
[assembly: CLSCompliantAttribute(true)]

namespace CXMLStudioIDE.Common.EntityProperties
{
    // Class marked as compliant.
    [CLSCompliantAttribute(true)]
    public class PropertySet : CollectionBase, ICustomTypeDescriptor
    {
        private DisplayContext _DisplayContext;

        #region Method Implimentations
        /// <summary>
        /// Add EntityName to access the class
        /// </summary>
        private string sEntityName = string.Empty;

        /// <summary>
        /// Add EntityProperty to Collectionbase List
        /// </summary>private string sEntityName = 
        /// <param name="Value"></param>
        public void Add(EntityProperty Value)
        {
            base.List.Add(Value);
        }

        /// <summary>
        /// Remove item from List
        /// </summary>
        /// <param name="Name"></param>
        public void Remove(string Name)
        {
            foreach (EntityProperty prop in base.List)
            {
                if (prop.Name == Name)
                {
                    base.List.Remove(prop);
                    return;
                }
            }
        }

        /// <summary>
        /// Indexer
        /// </summary>
        public EntityProperty this[int index]
        {
            get
            {
                return (EntityProperty)base.List[index];
            }
            set
            {
                base.List[index] = (EntityProperty)value;
            }
        }

        /// <summary>
        /// Indexer by Name
        /// </summary>
        public EntityProperty this[string strPropertyName]
        {
            get
            {
                foreach (EntityProperty entity in base.List)
                {
                    if (entity.Name == strPropertyName)
                        return entity;
                }
                return null;
            }
        }

        /// <summary>
        /// Save Control Properties in Database
        /// </summary>
        public void SaveControlPropertiesInDB(DisplayContext context, int i_EntityTypeVersionsID)
        {
            try
            {
                PropertiesDAL propdal = new PropertiesDAL();
                foreach (EntityProperty EP in this)
                {
                    string value = "";
                    if (EP.Value != null)
                    {
                        value = EP.Value.ToString();
                    }
                    propdal.SavePropertyValue(Convert.ToInt32(context), i_EntityTypeVersionsID,
                                                    EP.Name, value, EP.Category, EP.Type.FullName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region Constructor Implementation
            public PropertySet(string sEntityName)
            {
                this.sEntityName = sEntityName;
            }
        #endregion

        #region Property Implementation
            public string EntityName
            {
                get
                {
                    return this.sEntityName;
                }
            }
            public DisplayContext DisplayContext
            {
                get { return _DisplayContext; }
                set { _DisplayContext = value; }
            }
        #endregion

        #region "TypeDescriptor Implementation"
            /// <summary>
        /// Get Class Name
        /// </summary>
        /// <returns>String</returns>
        public String GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        /// <summary>
        /// GetAttributes
        /// </summary>
        /// <returns>AttributeCollection</returns>
        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        /// <summary>
        /// GetComponentName
        /// </summary>
        /// <returns>String</returns>
        public String GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        /// <summary>
        /// GetConverter
        /// </summary>
        /// <returns>TypeConverter</returns>
        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        /// <summary>
        /// GetDefaultEvent
        /// </summary>
        /// <returns>EventDescriptor</returns>
        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        /// <summary>
        /// GetDefaultProperty
        /// </summary>
        /// <returns>PropertyDescriptor</returns>
        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        /// <summary>
        /// GetEditor
        /// </summary>
        /// <param name="editorBaseType">editorBaseType</param>
        /// <returns>object</returns>
        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptor[] newProps = new PropertyDescriptor[this.Count];
            for (int i = 0; i < this.Count; i++)
            {
                EntityProperty prop = (EntityProperty)this[i];
                newProps[i] = new EntityPropertyDescriptor(ref prop, attributes);
            }

            return new PropertyDescriptorCollection(newProps);
        }

        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }
        #endregion
    }
}
